package top.chenyaohua.http;

import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * HTTP/1.1 200 OK
 * Bdpagetype: 3
 * Bdqid: 0xe6c5b9830013e4b7
 * Cache-Control: private
 * Connection: keep-alive
 * Content-Encoding: gzip
 * Content-Type: text/html;charset=utf-8
 * Cxy_all: baiduhome_pg+c0c7771179aa54abe9e2bee0c910b7ad
 * Cxy_ex: 1585506060+158758513+43f80bac3e0d05486e2ca69fa22aa005
 * Date: Sun, 29 Mar 2020 18:21:00 GMT
 * Is_status: 0
 * Server: BWS/1.1
 * Set-Cookie: delPer=0; path=/; domain=.baidu.com
 * Set-Cookie: BD_CK_SAM=1;path=/
 * Set-Cookie: PSINO=6; domain=.baidu.com; path=/
 * Set-Cookie: BDSVRTM=171; path=/
 * Set-Cookie: H_PS_PSSID=1434_21115_31187_30903_30823_31086; path=/; domain=.baidu.com
 * Strict-Transport-Security: max-age=172800
 * Traceid: 1585506060027141377016628901171428582583
 * Vary: Accept-Encoding
 * X-Ua-Compatible: IE=Edge,chrome=1
 * Transfer-Encoding: chunked
 */
public class HttpResponse {

    private OutputStream outputStream;

    private InputStream inputStream;


    /**
     * 协议
     */
    private String agreement;
    /**
     * 版本
     */
    private String version;
    /**
     * 状态码
     */
    private String status;
    /**
     * 状态描述
     */
    private String statusDescribe;
    /**
     * 头信息
     */
    private HashMap<String,String> header = new HashMap<>();


    public HttpResponse (OutputStream outputStream, InputStream inputStream, String agreement, String version) {
        this.outputStream = outputStream;
        this.inputStream = inputStream;
        this.agreement = agreement;
        this.version = version;
    }

    @Override
    public String toString () {

        StringBuffer sbf = new StringBuffer();
        //状态行
        sbf.append(String.format("%s/%s %s %s\r\n",this.agreement,this.version,this.status,this.statusDescribe));
        //头信息
        Set<Map.Entry<String, String>> entries = header.entrySet();
        for (Map.Entry<String, String> entry : entries) {

            sbf.append(String.format("%s: %s\r\n",entry.getKey(),entry.getValue()));

        }
        //空行
        sbf.append("\r\n");

        return  sbf.toString();
    }

    public OutputStream getOutputStream () {
        return outputStream;
    }

    public InputStream getInputStream () {
        return inputStream;
    }

    public String getAgreement () {
        return agreement;
    }

    public String getVersion () {
        return version;
    }

    public String getStatus () {
        return status;
    }

    public String getStatusDescribe () {
        return statusDescribe;
    }

    public HashMap<String, String> getHeader () {
        return header;
    }

    public void setStatus (String status) {
        this.status = status;
    }

    public void setStatusDescribe (String statusDescribe) {
        this.statusDescribe = statusDescribe;
    }
}
